﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Lync.Model;
using Microsoft.Lync.Utilities;


namespace LAura
{
    class Lync
    {
        private LyncClient m_lyncClient;
        private String m_lyncStatus;

        public Lync()
        {
            
        }

        public String GetLyncStatus() 
        {
            LoadLyncStatus();
            return m_lyncStatus;
        }


        public int LoadLync()
        {
            //Listen for events of changes in the state of the client
            try
            {
                m_lyncClient = LyncClient.GetClient();
                return 0;
            }
            catch (Exception e)
            {
                if (e is ClientNotFoundException || e is NotStartedByUserException || e is LyncClientException)
                {
                    Console.WriteLine(e);
                }
                else if (e is SystemException)
                {
                    // Log the exception thrown by the Lync Model API.
                    Console.WriteLine("Error: " + e);
                }
                else
                {
                    // ...
                }

                return 1;
            }
        }


        private void LoadLyncStatus()
        {
            //Get the current availability value from Lync
            try
            {
                ContactAvailability currentAvailability = (ContactAvailability)
                                                          m_lyncClient.Self.Contact.GetContactInformation(
                                                              ContactInformationType.Availability);
                m_lyncStatus = currentAvailability.ToString();
            }
            catch (LyncClientException e)
            {
                Console.WriteLine(e);
                m_lyncStatus = "error";
            }
            catch (SystemException systemException)
            {
                // Log the exception thrown by the Lync Model API.
                Console.WriteLine("Error: " + systemException);
                m_lyncStatus = "error";
            }
        }
    }
}
